#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2019/5/13 10:48
# @USER    : 717-3
# @File    : LongestSubstring.py
# @Software: PyCharm
# @Version  : Python-3.6
# @TASK:Longest Substring Without Repeating Characters


class Solution:
    def lengthOfLongestSubstring(self, s: str) -> int:
        """
        Given a string, find the length of the longest substring without repeating characters.

        :param s: string
        :return: the length of the longest substring

        Example 1:
            Input: "abcabcbb"

            Output: 3

            Explanation: The answer is "abc", with the length of 3.

        Example 2:
            Input: "bbbbb"

            Output: 1

            Explanation: The answer is "b", with the length of 1.

        Example 3:
            Input: "pwwkew"

            Output: 3

            Explanation: The answer is "wke", with the length of 3.

            Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
        """
        if len(s) == 0:
            return 0
        substring = s[0]
        max_len = 1
        for element in s[1:]:
            if element in substring:
                substring = substring[substring.index(element)+1:]
            substring = substring + element
            if len(substring) > max_len:
                max_len = len(substring)
        return max_len


if __name__ == '__main__':
    # Q3
    S = Solution()
    sample = 'pwwkew'
    print(S.lengthOfLongestSubstring(sample))
    print('done')
